﻿using System;
using System.Web;
using System.Text.RegularExpressions;
using CPETwit.Web.Service.Implementation;
using CPETwit.Web.Service.Interface;
using CPETwit.Data.Entity;


namespace CPETwit.Web.Pages
{
    public partial class MaPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            IAuthentificationService authentificationService = new AuthentificationService();
            ITwitterService twitterService = new TwitterService();

            bool isCorrect = authentificationService.CheckLoginAndPassword(this.UserNameSignin.Text, this.PasswordSignin.Text);

            switch (isCorrect)
            {
                case true :
                    Session.Add("login", twitterService.GetUser(this.UserNameSignin.Text));
                    Response.Redirect("timeline.aspx");
                    break;
                case false :
                    /* Activer le label erreur sur l'ASPX */
                    this.ErrorSignin.Text = "Identifiants inexistants";
                    this.ErrorSignup.Text = "";
                    break;
            }
        }

        protected void SignupButton_Click(object sender, EventArgs e)
        {
            bool isValid = true;
            ITwitterService twitterService = new TwitterService();
            this.ErrorSignin.Text = "";
            
            /* 1er test : pas de champs vides */
            if (String.IsNullOrEmpty(this.UserNameSignup.Text) || String.IsNullOrEmpty(this.PasswordSignup.Text) || String.IsNullOrEmpty(this.EmailSignup.Text))
            {
                isValid = false;
                this.ErrorSignup.Text = "Veuillez remplir tous les champs";
            }

            /* 2ème test : pas de champs contenant que des espaces */
            if (isValid && (String.IsNullOrWhiteSpace(this.UserNameSignup.Text) || String.IsNullOrWhiteSpace(this.EmailSignup.Text) || String.IsNullOrWhiteSpace(this.PasswordSignup.Text)))
            {
                isValid = false;
                this.ErrorSignup.Text = "Compléter les champs avec des caractères";
            }

            /* 3ème test : format d'email */
            if (isValid & !Regex.IsMatch(this.EmailSignup.Text, 
                  @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + 
                  @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$", 
                  RegexOptions.IgnoreCase))
            {
                isValid = false;
                this.ErrorSignup.Text = "Format d'email incorrect";
            }

            /* 4ème test : login déjà existant ? */
            if (isValid && ( !twitterService.IsUserNameValid(this.UserNameSignup.Text) || !twitterService.IsEmailValid(this.EmailSignup.Text) )) 
            {
                isValid = false;
                this.ErrorSignup.Text = "Login déjà existant";
            }

            if (isValid)
            {
                /* Inscription de l'utilisateur */
                twitterService.CreateUser(this.UserNameSignup.Text, this.PasswordSignup.Text, this.EmailSignup.Text);
                /* On loggue l'utilisateur */
                User u = twitterService.GetUser(this.UserNameSignup.Text);
                Session.Add("login", u);
                Response.Redirect("timeline.aspx");
            }
        }
    }
}